package cn.kjcoder.redis.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hkj
 * @email 1349091387@qq.com
 * @date 2022/1/21
 * 源码允许修改，并且本人十分乐意有 大佬 来对代码进行 随 意 调 教
 **/
@Slf4j
@Service
public class InitPrizeService {

    @Autowired
    private RedisTemplate redisTemplate;
    public static final String PRIZE_KEY = "jd:prize";

    /**
     * 奖品初始化
     */
    @PostConstruct
    public void init(){
        log.info("奖品初始化");
        Boolean hasKey = redisTemplate.hasKey(PRIZE_KEY);
        if(!hasKey){
            //如果redis不存在指定的key才进行奖品的初始化
            List<String> prize = this.prize();
            prize.forEach(t->this.redisTemplate.opsForSet().add(PRIZE_KEY,t));
        }
    }

    /**
     * 按一定的概率初始化奖品
     */
    public List<String> prize(){
        List<String> list = new ArrayList<>();
        //10个觅币  概率为10%
        for (int i = 0; i < 10; i++) {
            list.add("10-" + i);
        }
        //5个觅币  概率为20%
        for (int i = 0; i < 20; i++) {
            list.add("5-" + i);
        }
        //1个觅币  概率为60%
        for (int i = 0; i < 60; i++) {
            list.add("1-" + i);
        }
        //0个觅币  概率为10%
        for (int i = 0; i < 10; i++) {
            list.add("0-" + i);
        }
        return list;
    }
}
